<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools
          :nodedata="company"
          :root="true"
          @addDept="addDeptfn"
        />
        <!-- 主体树形组织 -->
        <el-tree v-loading="loading" :data="departs" :props="defaultProps" default-expand-all>
          <!-- node data是el-tree内部插槽提供出来的数据 -->
          <!-- node是偏底层一点的节点信息, 不需要 -->
          <!-- data是我们之前自己写的数据 -->
          <template #default="{ data }">
            <TreeTools
              :nodedata="data"
              @del-depts="getDepartments"
              @addDept="addDeptfn"
              @edit-dept="editDeptfn"
            />
          </template>
        </el-tree></el-card>
      <AddDept
        ref="addDept"
        :is-show.sync="isShow"
        :nodedata="nodedata"
        :nodelist="nodelist"
        @refresh="getDepartments"
      />
      <!-- @closeDialog="closeDialogfn" -->

      <!--
  :on-success="handleSuccess" 上传成功时 触发这个解析函数
  :before-upload="beforeUpload" 上传前 触发这个校验函数
 -->
      <UploadExcel :on-success="handleSuccess" :before-upload="beforeUpload" />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/TreeTools.vue'
import { reqGetDepartments } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import AddDept from './components/AddDept.vue'
export default {
  name: 'Departments',
  components: { TreeTools, AddDept },
  data() {
    return {
      departs: [],
      defaultProps: {
        // children: 'child',
        label: 'name'
      },
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      isShow: false,
      nodedata: {},
      nodelist: [],
      loading: true
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const { data: { depts }} = await reqGetDepartments()
      // console.log(depts)
      this.nodelist = depts
      const res = transListToTreeData(depts, '')
      this.departs = res
      this.loading = false
    },
    // closeDialogfn(flag) {
    //   this.isShow = flag
    // },
    addDeptfn(nodedata) {
      this.isShow = true
      // 传过来的数据先存一下
      this.nodedata = nodedata
      // 希望弹框显示才发请求
      // this.$refs.addDept.GetSImpleUserList()
      // this.$refs.addDept.dialogTitle = '添加子部门'
    },
    editDeptfn(nodedata) {
      this.isShow = true
      // 把当前部门数据存下来,将来用于部门信息的回显
      this.nodedata = nodedata
      // this.$refs.addDept.dialogTitle = '编辑部门'
      this.$refs.addDept.GetDepartDetaiById(nodedata.id)
    }
    // handDel() {
    //   this.getDepartments()
    // }

  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
